/**
 * 游戏管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 时间倒计时的进度条
         */
        timeProgress: cc.ProgressBar,
        mainManager: {
            default: null,
            visible: false,
        },
        /**
         * 攻击按钮
         */
        attackBtn: cc.Button,
        /**
         * 格挡按钮
         */
        defenceBtn: cc.Button,
        /**
         * 集气按钮
         */
        collectBtn: cc.Button,
        /**
         * 绝招按钮
         */
        exploseBtn: cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    update (dt) {
        if (this._isCountingTime) {
            //开始计时
            this.timeProgress.progress -= dt / GameData.CountingTime;
            if (this.timeProgress.progress <= 0) {
                this._isCountingTime = false;
            }
        }
    },

    /**
     * 初始化
     */
    init () {
        //当前回合数
        this._currentTurn = 1;
        this.setUpTimeDown();
        this.setButtonUnable();
        this.showEnableButton();
    },

    /**
     * 设置所有的按钮为不可点击状态
     */
    setButtonUnable () {
        this.attackBtn.interactable = false;
        this.defenceBtn.interactable = false;
        this.collectBtn.interactable = false;
        this.exploseBtn.interactable = false;
    },

    /**
     * 显示所有可以点击的按钮
     */
    showEnableButton () {
        //能量大于2可以攻击
        if (PlayerData.Energy >= 2) {
            this.attackBtn.interactable = true;
        }
        //能量大于5可以放大招
        if (PlayerData.Energy >= 2) {
            this.exploseBtn.interactable = true;
        }
        //其余无能量要求
        this.defenceBtn.interactable = true;
        this.collectBtn.interactable = true;

    },

    /**
     * 启动时间倒计时
     */
    setUpTimeDown () {
        this.timeProgress.progress = 1;
        this._isCountingTime = true;
    },

    /**
     * 攻击按钮
     */
    onAttackClick () {
        this.mainManager.sendSocketMessage({PlayerAction: {
            RoomNo: GameData.RoomNo,
            OpenId: GameData.OpenID,
            Type: 0,
        }});
        this.setButtonUnable();
    },

    /**
     * 格挡按钮
     */
    onDefenceClick () {
        this.mainManager.sendSocketMessage({PlayerAction: {
            RoomNo: GameData.RoomNo,
            OpenId: GameData.OpenID,
            Type: 1,
        }});
        this.setButtonUnable();
    },

    /**
     * 集气按钮
     */
    onCollectClick () {
        this.mainManager.sendSocketMessage({PlayerAction: {
            RoomNo: GameData.RoomNo,
            OpenId: GameData.OpenID,
            Type: 2,
        }});
        this.setButtonUnable();
    },

    /**
     * 大招按钮
     */
    onExploseClick () {
        this.mainManager.sendSocketMessage({PlayerAction: {
            RoomNo: GameData.RoomNo,
            OpenId: GameData.OpenID,
            Type: 3,
        }});
        this.setButtonUnable();
    },
});
